package com.bwie.controller;

import com.bwie.pojo.TbMenu;
import com.bwie.pojo.TbUser;
import com.bwie.service.TbUserService;
import com.bwie.utils.R;
import com.bwie.utils.Result;
import com.bwie.vo.LoginVo;
import com.bwie.vo.PageInfoVo;
import io.jsonwebtoken.Jwts;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.sound.sampled.Line;
import java.util.List;

/**
 * @projectName: server-2211a-shop
 * @package: com.bwie.controller
 * @className: IndexController
 * @author: 杨珂
 * @description: TODO
 * @date: 2025/5/21 13:45
 * @version: 1.0
 */
@RestController
@RequestMapping("/auth/index")
@Slf4j
public class IndexController {

    @Autowired
    TbUserService tbUserService;

    @PostMapping("/login")
    public Result login(@RequestBody LoginVo loginVo) {
        log.info("进入login方法");
        return tbUserService.login(loginVo);
    }

    /**
     * @param request:
     * @return Result
     * @author :杨珂
     * @description 获取权限菜单
     * @date 2025/5/22 16:50
     */
    @PostMapping("/getMenuList")
    public Result getMenuList(HttpServletRequest request) {

        //获取token
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error().setMsg("请先登录");
        }

        Integer userId = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody().get("userId", Integer.class);

        List tbMenuList = tbUserService.getMenuList(userId);


        return Result.ok().setData(tbMenuList);

    }

    @PostMapping("/getUserlist")
    public Result getUserList(@RequestBody PageInfoVo pageInfoVo) {
        return tbUserService.getUserList(pageInfoVo);
    }

    /**
     * @param tbUser:
     * @return Result
     * @author :杨珂
     * @description 根据userId修改
     * @date 2025/5/22 16:49
     */
    @PostMapping("/updateByUserId")
    public Result updateByUserId(@RequestBody TbUser tbUser) {
        return tbUserService.updateByUserId(tbUser);
    }

    /**
     * @param file:
     * @return Result
     * @author :杨珂
     * @description 图片上传
     * @date 2025/5/22 16:49
     */
    @PostMapping("/uploadImage")
    public Result uploadImage(@RequestPart("file") MultipartFile file) {
        return tbUserService.uploadImage(file);
    }


    /**
     * @param userId:
     * @return R
     * @author :杨珂
     * @description 根据ID删除
     * @date 2025/5/22 16:49
     */
    @PostMapping("/deleteUser/{userId}")
    public Result deleteUser(@PathVariable("userId") Integer userId) {
        return tbUserService.deleteUser(userId);
    }

    @PostMapping("/deleteUserAll")
    public Result deleteUserAll(@RequestBody List<TbUser> tbUserList) {
        return tbUserService.deleteUserAll(tbUserList);
    }


    @PostMapping("/checkUser")
    public Result checkUser(@RequestBody TbUser tbUser) {
        return tbUserService.checkUser(tbUser);
    }
}
